package com.qf.fmw.dao.impl;

import com.qf.fmw.dao.AddressDao;
import com.qf.fmw.entity.bo.AddressBo;
import com.qf.fmw.entity.bo.OrderBo;
import com.qf.fmw.utils.DBManager;

import java.util.List;

public class AddressDaoImpl implements AddressDao {
    // 查询所有地址
    @Override
    public List<AddressBo> selectAllAddresses() {
        return DBManager.commonQuery("select a_id, a_name, a_phone, a_detail, a_state from address where is_delete=1", AddressBo.class);
    }

    //  模糊查询
    @Override
    public List<AddressBo> searchAddress(String query) {
        return DBManager.commonQuery("select a_id, a_name, a_phone, a_detail, a_state from address where is_delete=1 and a_name like ? or a_phone like ?", AddressBo.class, "%" + query + "%", "%" + query + "%");
    }

    //  删除地址
    @Override
    public Integer deleteAddress(int addressId) {
        //伪删除
        return DBManager.commonUpdate("update address set is_delete=0 where a_id=?", addressId);
    }

    //添加地址
    @Override
    public boolean addAddress(String name, String phone, String detail, int state) {
        int result = DBManager.commonUpdate("INSERT INTO address(a_name, a_phone, a_detail, a_state) VALUES (?, ?, ?, ?)", name, phone, detail, state);
        //影响行数大于0
        return result > 0;
    }

    //修改地址
    @Override
    public boolean updateAddress(int addressId, String name, String phone, String detail, int state) {
        int result = DBManager.commonUpdate("UPDATE address SET a_name = ?, a_phone = ?, a_detail = ?, a_state = ? WHERE a_id = ?", name, phone, detail, state, addressId);
        //影响行数大于0
        return result > 0;
    }
}
